﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WapYlPlaceOrder.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WapYlPlaceOrder.cs
// 创建时间：2014-12-25 12:59
// 修改时间：2014-12-24 14:27
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace Payment.WapYinlian
{
    #region

    using System;
    using System.Collections.Generic;

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    using WebCommon;
  

    #endregion

    /// <summary>
    /// The wap yl place Service.
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class WapYlPlaceOrder : PaySubBase
    {
        #region Fields

        /// <summary>
        /// The wap yl config.
        /// </summary>
        [JsonIgnore]
        private readonly WapYlConfig wapYlConfig;

        /// <summary>
        /// The pay Service.
        /// </summary>
        [JsonIgnore]
        private IPayOrder payOrder;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WapYlPlaceOrder"/> class. 
        /// 下单模型构造函数
        /// </summary>
        /// <param name="wapYlConfig">
        /// </param>
        /// <param name="payOrder">
        /// </param>
        public WapYlPlaceOrder(WapYlConfig wapYlConfig, IPayOrder payOrder)
        {
            this.TransCode = "201201";
            this.OrderTime = payOrder.CreateDateTime().ToString("HHmmss");
            this.OrderDate = payOrder.CreateDateTime().ToString("yyyyMMdd");
            this.TransAmt = payOrder.GetTotalFeeInt().ToString();
            this.MerId = wapYlConfig.MerId;
            this.MerOrderId = payOrder.GetId();
            this.MerTermId = wapYlConfig.MerTermId;
            this.Gateway = "http://116.228.21.162:9127/umsFrontWebQmjf/infoCheckPay!getOrderCheck.ac";
            this.TransType = "NoticePay";
            this.EffectiveTime = "0";
            this.OrderDesc = payOrder.GetSubject();

            // 备注
            this.Reserve = payOrder.GetOrderBody();

            // 商户号
            this.NotifyUrl = wapYlConfig.NotifyUrl;
            this.wapYlConfig = wapYlConfig;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     订单有效期	N	12	O	订单有效期（秒），0 为长期有效
        /// </summary>
        [JsonProperty]
        public string EffectiveTime { get; private set; }

        /// <summary>
        ///     商户号	N	32	M	（由银商分配）
        /// </summary>
        [JsonProperty]
        public string MerId { get; private set; }

        /// <summary>
        ///     商户订单号	AN	32	M	商户为某笔交易生成的唯一识别号，在一天内不得重复。
        /// </summary>
        [JsonProperty]
        public string MerOrderId { get; private set; }

        /// <summary>
        ///     签名数据	ANS	256	M	商户私钥对指定非空参数按顺序拼接进行签名，转为HexStr
        /// </summary>
        [JsonProperty]
        public string MerSign { get; private set; }

        /// <summary>
        ///     终端号	N	8	M	（由银商分配）
        /// </summary>
        [JsonProperty]
        public string MerTermId { get; private set; }

        /// <summary>
        ///     通知url	ANS	256	O	商户后台接收支付结果成功通知地址，保证外网可以访问
        /// </summary>
        [JsonProperty]
        public string NotifyUrl { get; private set; }

        /// <summary>
        ///     订单时间	N	14	M	格式： hhmmss
        /// </summary>
        [JsonProperty]
        public string OrderDate { get; private set; }

        /// <summary>
        ///     订单描述	ANS	256	M
        /// </summary>
        [JsonProperty]
        public string OrderDesc { get; private set; }

        /// <summary>
        ///     订单日期	N	8	M	格式：yyyyMMdd
        /// </summary>
        [JsonProperty]
        public string OrderTime { get; private set; }

        /// <summary>
        ///     备用字段	ANS	256	O	备用字段
        /// </summary>
        [JsonProperty]
        public string Reserve { get; set; }

        /// <summary>
        ///     交易金额	N	12	M	单位分
        /// </summary>
        [JsonProperty]
        public string TransAmt { get; private set; }

        /// <summary>
        ///     交易代码	N	6	M	固定值：201201
        /// </summary>
        [JsonProperty]
        public string TransCode { get; private set; }

        /// <summary>
        ///     交易类型	AN	32	M	固定值：NoticePay
        /// </summary>
        [JsonProperty]
        public string TransType { get; private set; }

        #endregion

        #region Public Methods and Operators

        protected override string Method()
        {
            return "post";
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string Fasongjiaoyi()
        {
            //var httpHelper = new HttpHelper();
            //httpHelper.Url = this.wapYlConfig.PostUrlXiadan;
            //httpHelper.Data = JsonConvert.SerializeObject(this);
            //httpHelper.Method = true;

            return HttpHelper.PostPage(this.wapYlConfig.PostUrlXiadan, "&jsonString=" + JsonConvert.SerializeObject(this));
        }

        protected override Dictionary<string, string> GetSubData()
        {
            SetMerSign();
            var fasongjiaoyi = Fasongjiaoyi();
             
                var wapYlRequestPlaceOrder = JObject.Parse(fasongjiaoyi).ToObject<WapYlRequestPlaceOrder>();
                string getmerSign = wapYlRequestPlaceOrder.GetmerSign(this.wapYlConfig);


          return new Dictionary<string, string>
                                 {
                                     { "merSign", getmerSign },
                                     { "chrCode", wapYlRequestPlaceOrder.ChrCode },
                                     { "tranId", wapYlRequestPlaceOrder.TransId },
                                     { "url", this.wapYlConfig.RetUrl }
                                 };
            return null;
        }

        /// <summary>
        /// 获取发送前的数据
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetFasongJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        /// <summary>
        /// 生成签名
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string SetMerSign()
        {
            var str = this.OrderTime + this.EffectiveTime + this.OrderDate + this.MerOrderId + this.TransType
                      + this.TransAmt + this.MerId + this.MerTermId + this.NotifyUrl + this.Reserve + this.OrderDesc;

            this.MerSign = RsaUtil.RsaSign(str, this.wapYlConfig.Pvr);
            return this.MerSign;
        }


        #endregion
    }
}